package watermark;

import net.coobird.thumbnailator.Thumbnails;
import org.junit.Test;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 实现加水印图片的过程
 * @author syt
 */
public class PictureTool {

    private final static int DOWN_RIGHT = 0;
    private final static int DOWN_LEFT= 1;
    private final static int UP_RIGHT = 2;
    private final static int UP_LEFT= 3;
    private final static int MIDDLE = 4;
    private final static int UP_LEFT_TO_DOWN_RIGHT = 5;
    private final static int UP_RIGHT_TO_DOWN_LEFT = 6;

    // 实现加水印方法
    public static BufferedImage watermark(String iconPath, String filepath, int toward, float scale) throws IOException {
        return watermark(new File(iconPath), new File(filepath), toward, scale);
    }

    /**
     * 调整水印以适应图片的大小
     * @param image 图片
     * @param icon 水印文件
     * @return 修改后的水印
     * @throws IOException
     */
    @Test
    public static String watermarkWidthHeight(BufferedImage image, File icon) throws IOException {

        BufferedImage waterImg = ImageIO.read(icon);
        double bl = 0.18;//水印为原图宽占比
        //根据比例计算新的水印图宽高
        int waterWidth = (int) (image.getWidth() * bl);
        int waterHeight = waterWidth * waterImg.getHeight() / waterImg.getWidth();
        waterImg = Thumbnails.of(waterImg).size(waterWidth, waterHeight).keepAspectRatio(false).asBufferedImage();

        File file1 = new File("");
        String filePath1 = file1.getCanonicalPath()+File.separator+"watermark_media\\";
        String name = filePath1 + icon.getName().substring(icon.getName().lastIndexOf("\\") + 1);
        OutputStream os = new FileOutputStream(name);
        ImageIO.write(waterImg, "JPG", os);

        return name;

    }

    /**
     * 获取水印的高度和宽度
     * @param iconImg 水印图片路径
     * @return 水印的高度和宽度
     * @throws IOException
     */
    private static Point getPictureWidthAndHeight(Image iconImg) throws IOException {
        int Width = iconImg.getWidth(null);
        int Ascent = iconImg.getHeight(null);
        return new Point(Width, Ascent);
    }

    /**
     * 计算水印的位置
     * @param p 水印的高度和宽度
     * @param toward 放置位置
     * @param imgWidth 原图片的宽度
     * @param imgHeight 原图片的高度
     * @return 水印在原图中的位置
     */
    public static Point calculate(Point p, int toward, double imgWidth, double imgHeight){
        int x = 0, y = 0;
        if(toward == UP_LEFT_TO_DOWN_RIGHT){
            double hypotenuse = Math.sqrt(Math.pow(imgWidth, 2) + Math.pow(imgHeight, 2));
            double tempX = (p.x/2) * imgWidth/hypotenuse;
            double tempY = (p.x/2) * imgHeight/hypotenuse;
            x = (int) (imgWidth/2 - tempX);
            y = (int) (imgHeight/2 - tempY);
        }else if(toward == UP_RIGHT_TO_DOWN_LEFT){
            double hypotenuse = Math.sqrt(Math.pow(imgWidth, 2) + Math.pow(imgHeight, 2));
            double tempX = (p.x/2) * imgWidth/hypotenuse;
            double tempY = (p.x/2) * imgHeight/hypotenuse;
            x = (int) (imgWidth/2 - tempX);
            y = (int) (imgHeight/2 + tempY);
        }else if(toward == DOWN_RIGHT){
            x = (int)imgWidth - p.x;
            y = (int)imgHeight - p.y;
        }else if(toward == DOWN_LEFT){
            x = 0;
            y = (int)imgHeight - p.y;
        }else if(toward == UP_RIGHT){
            x = (int)imgWidth - p.x;
            y = 0;
        }else if(toward == UP_LEFT){
            x = 0;
            y = 0;
        }else if(toward == MIDDLE){
            x = (int)imgWidth/2-p.x/2;
            y = (int)imgHeight/2;
        }
        return new Point(x, y);
    }

    /**
     * 添加水印
     * @param icon 水印文件
     * @param file 源文件
     * @param toward 水印添加位置
     * @param scale 原图片缩放比例
     * @return 添加水印后的图片
     * @throws IOException
     */
    public static BufferedImage watermark(File icon, File file, int toward, float scale) throws IOException {
        BufferedImage buffImg = ImageTool.getMyBufferedImage(file, scale);
        Graphics2D g2d = buffImg.createGraphics();
        g2d.setRenderingHints(ImageTool.getMyRenderingHints());

        String iconPath = watermarkWidthHeight(buffImg, icon);
        Image iconImg = ImageIO.read(new File(iconPath));

        double imgWidth = buffImg.getWidth();
        double imgHeight = buffImg .getHeight();

        //水印所需的宽和高
        Point p = getPictureWidthAndHeight(iconImg);

        //计算水印的起点
        Point p1 = calculate(p, toward, imgWidth, imgHeight);

        // 设置水印旋转
        //平移到指定位置，旋转指定弧度
        if(toward == UP_LEFT_TO_DOWN_RIGHT){
            g2d.rotate(Math.atan(imgHeight/imgWidth), //Math.toRadians(135)
                    p1.x, p1.y);
        }else if(toward == UP_RIGHT_TO_DOWN_LEFT){
            g2d.rotate(-Math.atan(imgHeight/imgWidth), //Math.toRadians(135)
                    p1.x, p1.y);
        }

        g2d.drawImage(iconImg, p1.x, p1.y, null);
        g2d.dispose();// 释放图形上下文使用的系统资源
        return buffImg;
    }
}

